package com.health.service.impl;

import com.health.constant.MessageConstant;
import com.health.entity.Result;
import com.health.mapper.MemberMapper;
import com.health.mapper.OrderMapper;
import com.health.mapper.OrderSettingMapper;
import com.health.pojo.Member;
import com.health.pojo.Order;
import com.health.pojo.OrderSetting;
import com.health.service.OrderService;
import com.health.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public synchronized Result order(Map map) throws Exception{
        String orderDate=(String) map.get("orderDate");
        OrderSetting orderSetting=orderSettingMapper.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if(orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        int number=orderSetting.getNumber();
        int reservations=orderSetting.getReservations();
        if(reservations>=number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        String telephone=(String) map.get("telephone");
        Member member=memberMapper.findByTelephone(telephone);
        if(member!=null){
            Integer memberId=member.getId();
            Date order_Date=DateUtils.parseString2Date(orderDate);
            String setmealId=(String) map.get("setmealId");
            Order order=new Order(memberId,order_Date,Integer.parseInt(setmealId));
            List<Order> list=orderMapper.findByCondition(order);
            if(list!=null&&list.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            member=new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberMapper.add(member);
        }
        Order order=new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderMapper.add(order);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingMapper.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception{
        Map map=orderMapper.findById4Detail(id);
        if(map!=null){
            Date orderDate=(Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
